package org.ffmmx.ybb.controller;

import javafx.scene.transform.Rotate;
import org.ffmmx.ybb.model.*;
import org.ffmmx.ybb.repo.CityRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
public class HelloWorldController {
    @Autowired
    private CityRepo cityRepo;
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index() {
        System.out.println("Hello World");
        test();
        ffff();
        kkk();
       return "Hello World";
    }
    @RequestMapping("/sayHello")
    public Result<HelloWorldPerson> sayHello(@RequestParam(value = "name", defaultValue = "World")
                                                 String somebody) {
        return new Ok<HelloWorldPerson>(new HelloWorldPerson(somebody));
    }

    @RequestMapping(value = "/cities",method = RequestMethod.GET)
    public Result<City> queryCities(@RequestParam(required = false) String name,@RequestParam(required = false) String state,@RequestParam(required = false) String country,@RequestParam(required = false) String map,@RequestParam(required = false) Long id) {
        Pageable pageable=new PageRequest(0,29);
        final City city = new City();
        city.setCountry(country);
        city.setId(id);
        city.setMap(map);
        city.setName(name);
        city.setState(state);
        Page<City> cityPage =  cityRepo.findAll(new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                final List<Predicate> predicates = new ArrayList<>();

                if(!StringUtils.isEmpty(country)){
                    predicates.add(criteriaBuilder.equal(root.get("country"),country));
                }
                if(id!=null && id!=0){
                    predicates.add(criteriaBuilder.equal(root.get("id"),id));
                }
                if(!StringUtils.isEmpty(map)){
                    predicates.add(criteriaBuilder.equal(root.get("map"),map));
                }
                if(!StringUtils.isEmpty(name)){
                    predicates.add(criteriaBuilder.equal(root.get("name"),name));
                }
                if (!StringUtils.isEmpty(state)) {
                    predicates.add(criteriaBuilder.equal(root.get("state"),state));
                }

                return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
            }
        }, pageable);
        return new PageResult(cityPage);
    }
    public void test(){
        System.out.println(" TEST");
        System.out.println("KKKK");
    }

    public void ffff(){
        System.out.println("fff");
    }

    public void kkk(){
        System.out.println("kkk");
    }
}
